package net.cyndeline.GameMap.dataStructure.examinators

import net.cyndeline.GameMap.GridNodeExaminer
import net.cyndeline.MovementDirection
import net.cyndeline.NorthEast
import net.cyndeline.South
import net.cyndeline.East
import net.cyndeline.SouthEast
import net.cyndeline.SouthWest
import net.cyndeline.West
import net.cyndeline.NorthWest
import net.cyndeline.North
import net.cyndeline.GameMap.Tile

/**
 * Gives relative properties for grid nodes that lie on a 2-dimensional
 * grid and have 4 sides.
 */
class SquareNodeExaminer extends GridNodeExaminer {
  
  override def canConnectTo(node: Tile, connectTo: Tile): Boolean = {
    ((node != connectTo) 
    && ((node.x != connectTo.x) || (node.y != connectTo.y)) 
    && (Math.abs(node.x - connectTo.x) <= 1) 
    && (Math.abs(node.y - connectTo.y) <= 1))
  }
  
  override def getDirection(node: Tile, comparedTo: Tile): Option[MovementDirection] = {
    node match {
    	case n: Tile if (n.x < comparedTo.x && n.y < comparedTo.y) => return Option(NorthWest)
    	case n: Tile if (n.x < comparedTo.x && n.y == comparedTo.y) => return Option(West)
    	case n: Tile if (n.x < comparedTo.x && n.y > comparedTo.y) => return Option(SouthWest)
    	case n: Tile if (n.x == comparedTo.x && n.y < comparedTo.y) => return Option(North)
    	case n: Tile if (n.x == comparedTo.x && n.y > comparedTo.y) => return Option(South)
    	case n: Tile if (n.x > comparedTo.x && n.y < comparedTo.y) => return Option(NorthEast)
    	case n: Tile if (n.x > comparedTo.x && n.y == comparedTo.y) => return Option(East)
    	case n: Tile if (n.x > comparedTo.x && n.y > comparedTo.y) => return Option(SouthEast)
    	case _ => return None
    }
  }
  
  def getNeighborPositions(node: Tile): List[(Int, Int)] = {
    var positions = List[(Int, Int)]()
	  
    positions ::= (node.x - 1, node.y - 1)
    positions ::= (node.x, node.y - 1)
    positions ::= (node.x + 1, node.y - 1)
    positions ::= (node.x - 1, node.y)
    positions ::= (node.x + 1, node.y)
    positions ::= (node.x - 1, node.y + 1)
    positions ::= (node.x, node.y + 1)
    positions ::= (node.x + 1, node.y + 1)
    
    positions
  }
}